package guilayer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import modellayer.KeyPerson;
import modellayer.ZipCode;
import controllerlayer.AppointmentCtrl;
import controllerlayer.KeyPersonCtrl;
import controllerlayer.TimestampCtrl;
import controllerlayer.ZipCodeCtrl;
import java.awt.Color;

public class CreateAppointment extends JFrame
{	
	private JPanel contentPane;
	private JTextField txtAddress;	
	private JTextField txtZipCode;	
	private TimestampCtrl tc = new TimestampCtrl();	
	private ZipCodeCtrl zcc = new ZipCodeCtrl();
	private ArrayList<Integer> quarters = new ArrayList<Integer>();
	private ArrayList<Integer> hours = new ArrayList<Integer>();
	private ArrayList<Integer> days = new ArrayList<Integer>();
	private ArrayList<Integer> months = new ArrayList<Integer>();
	private ArrayList<Integer> years = new ArrayList<Integer>();
	private JTextArea txtAreaSubject;
	private JComboBox cbFromDay;
	private JComboBox cbFromMonth;
	private JComboBox cbFromYear;
	private JComboBox cbKeyPerson;	
	private JComboBox cbFromHour;
	private JComboBox cbFromQuarter;
	private JComboBox cbToDay;
	private JComboBox cbToMonth;
	private JComboBox cbToYear;
	private JComboBox cbToHour;
	private JComboBox cbToQuarter;
	private int yearCountMin = tc.getYear() - 10;
	private int yearCountMax = tc.getYear() + 10;
	private int monthCount = 1;
	private int dayCount = 1;
	private int hourCount = 0;
	private int quarterCount = 00;
	private JTextField txtCity;
	private int zipLength = 0;
	private JLabel lblLblerror;
	
	/**
	 * Create the frame.
	 */	
	public CreateAppointment() 
	{
		/**
		 * Setup frame
		 */
		setTitle("Fairway Networks | Opret Aftale");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 475, 350);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		getContentPane().setLayout(null);
		contentPane.setLayout(null);
		
		
		/**
		 * Set up JLabels
		 */		
		JLabel lblKeyPerson = new JLabel("N\u00F8gleperson:");
		lblKeyPerson.setBounds(10, 11, 124, 14);
		getContentPane().add(lblKeyPerson);
		
		JLabel lblSubject = new JLabel("Emne:");
		lblSubject.setBounds(10, 165, 124, 14);
		getContentPane().add(lblSubject);
		
		JLabel lblFromDate = new JLabel("Fra:");
		lblFromDate.setBounds(10, 42, 124, 14);
		getContentPane().add(lblFromDate);
		
		JLabel lblToDate = new JLabel("Til:");
		lblToDate.setBounds(10, 73, 124, 14);
		contentPane.add(lblToDate);
		
		JLabel lblAddress = new JLabel("Addresse:");
		lblAddress.setBounds(10, 106, 124, 14);
		getContentPane().add(lblAddress);
		
		JLabel lblZipCodeCity = new JLabel("Postnummer/By:");
		lblZipCodeCity.setBounds(10, 137, 124, 14);
		getContentPane().add(lblZipCodeCity);
		
		/**
		 * Setup input areas
		 */		
		cbKeyPerson = new JComboBox(listOfKPNames());
		cbKeyPerson.setEditable(false);
		cbKeyPerson.setBounds(144, 8, 305, 20);
		contentPane.add(cbKeyPerson);
		
		cbFromDay = new JComboBox(listOfDays());
		cbFromDay.setToolTipText("Dag");
		cbFromDay.setEditable(false);
		cbFromDay.setSelectedItem(tc.getDay());
		cbFromDay.setBounds(144, 39, 53, 20);
		contentPane.add(cbFromDay);
		
		cbFromMonth = new JComboBox(listOfMonths());
		cbFromMonth.setToolTipText("M\u00E5ned");
		cbFromMonth.setEditable(false);
		cbFromMonth.setSelectedItem(tc.getMonth());
		cbFromMonth.setBounds(207, 39, 53, 20);
		contentPane.add(cbFromMonth);
		
		cbFromYear = new JComboBox(listOfYears());
		cbFromYear.setToolTipText("\u00C5r");
		cbFromYear.setEditable(false);
		cbFromYear.setSelectedItem(tc.getYear());
		cbFromYear.setBounds(270, 39, 53, 20);
		contentPane.add(cbFromYear);
		
		cbFromHour = new JComboBox(listOfHours());
		cbFromHour.setBounds(333, 39, 53, 20);
		contentPane.add(cbFromHour);
		
		cbFromQuarter = new JComboBox(listOfQuarters());
		cbFromQuarter.setBounds(396, 39, 53, 20);
		contentPane.add(cbFromQuarter);
		
		cbToDay = new JComboBox(listOfDays());
		cbToDay.setToolTipText("Dag");
		cbToDay.setEditable(false);
		cbToDay.setSelectedItem(tc.getDay());
		cbToDay.setBounds(144, 70, 53, 20);
		contentPane.add(cbToDay);
		
		cbToMonth = new JComboBox(listOfMonths());
		cbToMonth.setToolTipText("M\u00E5ned");
		cbToMonth.setEditable(false);
		cbToMonth.setSelectedItem(tc.getMonth());
		cbToMonth.setBounds(207, 70, 53, 20);
		contentPane.add(cbToMonth);
		
		cbToYear = new JComboBox(listOfYears());
		cbToYear.setToolTipText("\u00C5r");
		cbToYear.setEditable(false);
		cbToYear.setSelectedItem(tc.getYear());
		cbToYear.setBounds(270, 70, 53, 20);
		contentPane.add(cbToYear);

		cbToHour = new JComboBox(listOfHours());
		cbToHour.setBounds(333, 70, 53, 20);
		contentPane.add(cbToHour);
		
		cbToQuarter = new JComboBox(listOfQuarters());
		cbToQuarter.setBounds(396, 70, 53, 20);
		contentPane.add(cbToQuarter);
		
		txtAddress = new JTextField();
		txtAddress.setBounds(144, 103, 305, 20);
		getContentPane().add(txtAddress);
		txtAddress.setColumns(10);
		
		txtZipCode = new JTextField();
		txtZipCode.addKeyListener(new KeyListener() {
			
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				setCity();
				checkLength();
			}
			
			public void keyPressed(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		txtZipCode.setBounds(144, 134, 80, 20);
		getContentPane().add(txtZipCode);
		txtZipCode.setColumns(10);	
		
		txtCity = new JTextField();
		txtCity.setEditable(false);
		txtCity.setBounds(234, 134, 215, 20);
		contentPane.add(txtCity);
		txtCity.setColumns(10);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(144, 165, 305, 106);
		contentPane.add(scrollPane);		
		txtAreaSubject = new JTextArea();
		scrollPane.setViewportView(txtAreaSubject);
		txtAreaSubject.setLineWrap(true);			
		
		/**
		 * Setup buttons
		 */		
		JButton btnSave = new JButton("Gem");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (createAppointment()) 
					closeWindow();
			}
		});
		btnSave.setBounds(261, 282, 89, 23);
		contentPane.add(btnSave);
		
		JButton btnCancel = new JButton("Annuller");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeWindow();
			}
		});
		btnCancel.setBounds(360, 282, 89, 23);
		contentPane.add(btnCancel);	
		
		lblLblerror = new JLabel("");
		lblLblerror.setForeground(Color.RED);
		lblLblerror.setBounds(10, 286, 214, 14);
		contentPane.add(lblLblerror);
	}
	
	private void setCity()
	{
		try {
			if( zcc.findZipCode(Integer.parseInt(txtZipCode.getText())) != null)
			{
				int zipcode = Integer.parseInt(txtZipCode.getText());
				ZipCode zc = zcc.findZipCode(zipcode);
				
				txtCity.setText(zc.getCity());
			}	
		} catch (NumberFormatException e) {
			setError("Kun tal i postnr. feltet!");
		}
	}
	
	private void checkLength()
	{
		String string = txtZipCode.getText();
		if (string.length()>4) {
			txtZipCode.setText(string.substring(0, 4));
		}
	}

	private void closeWindow() 
	{
		dispose();
	}	
	
	private Object[] listOfKPNames()
	{
		KeyPersonCtrl kpc = new KeyPersonCtrl();
		ArrayList<KeyPerson> kpa = new ArrayList<KeyPerson>();
		
		kpa = kpc.listOfAllKeyPerson();
		return kpa.toArray();
	}
	
	private Object[] listOfDays()
	{		
		while(dayCount <= 31)
		{
			days.add(dayCount);
			dayCount++;
		}
		
		return days.toArray();
	}
	
	private Object[] listOfMonths()
	{
		while(monthCount <= 12)
		{
			months.add(monthCount);
			monthCount++;
		}
		
		return months.toArray();
	}
	
	private Object[] listOfYears()
	{
		while(yearCountMin <= yearCountMax)
		{
			years.add(yearCountMin);
			yearCountMin++;
		}
		
		return years.toArray();
	}
	
	private Object[] listOfHours()
	{		
		while(hourCount <= 23)
		{
			hours.add(hourCount);
			hourCount++;
		}
		
		return hours.toArray();
	}
	
	private Object[] listOfQuarters()
	{
		while(quarterCount <= 45)
		{
			quarters.add(quarterCount);
			quarterCount = quarterCount + 15;
		}
		
		return quarters.toArray();
	}
	
	private boolean createAppointment()
	{
		boolean success = false;
		AppointmentCtrl ac = new AppointmentCtrl();
		
		String subject;
		long fromDate;
		long toDate; 
		String address;
		int zipcode;
		long addedDate;
		KeyPerson kp;
		ZipCode zc;
		
		try
		{
			subject = txtAreaSubject.getText();		
			address = txtAddress.getText();
			zipcode = Integer.parseInt(txtZipCode.getText());
			zc = zcc.findZipCode(zipcode);
			addedDate = tc.getTimestampJava();
			kp = (KeyPerson) cbKeyPerson.getSelectedItem();
			fromDate = new TimestampCtrl( (Integer) cbFromYear.getSelectedItem(), (Integer) cbFromMonth.getSelectedItem(), (Integer) cbFromDay.getSelectedItem(), (Integer) cbFromHour.getSelectedItem(), (Integer) cbFromQuarter.getSelectedItem()).getTimestampJava();
			toDate = new TimestampCtrl( (Integer) cbToYear.getSelectedItem(), (Integer) cbToMonth.getSelectedItem(), (Integer) cbToDay.getSelectedItem(), (Integer) cbToHour.getSelectedItem(), (Integer) cbToQuarter.getSelectedItem()).getTimestampJava();
			
			if (!subject.isEmpty() && !address.isEmpty() && zc!=null) {
				success = true;
			} else {
				setError("Inkorrekte data!");
			}
			
			if (success) {
				ac.createAppointment(subject, fromDate, toDate, address, zc, addedDate, kp);
			}
		}
		catch(NumberFormatException e)
		{
			setError("Du har ikke indtastet et postnummer!");
		}	
		
		return success;
	}

	private void setError(String err) {
		lblLblerror.setText(err);
	}
}

